package org.aplikator.client.command;

import org.aplikator.client.Aplikator;
import org.aplikator.client.descriptor.ViewDTO;
import org.aplikator.client.rpc.Callback;
import org.aplikator.client.rpc.impl.GetView;
import org.aplikator.client.rpc.impl.GetViewResponse;
import org.aplikator.client.widgets.TableFactory;
import org.aplikator.client.widgets.TableInterface;

import com.google.gwt.user.client.ui.Widget;

@SuppressWarnings("serial")
public class ListEntities extends BindableCommand {

    @SuppressWarnings("unused")
    private ListEntities() {
    }

    public ListEntities(String viewId) {
        super();
        this.viewId = viewId;
    }

    protected ViewDTO view;

    private String viewId;

    @Override
    public void execute() {
        if (view == null) {
            Aplikator.aplikatorService.execute(new GetView(viewId), new Callback<GetViewResponse>() {
                public void process(GetViewResponse response) {
                    ListEntities.this.view = response.getView();
                    drawPanel();
                }
            });
        } else {
            drawPanel();
        }
    }

    private void drawPanel() {
        if (contents == null) {
            contents = createTable();
        }
        mainPanel.setContents(contents);


    }

    private Widget createTable() {

        TableInterface table = TableFactory.create(view.clone(), null, null);
        table.setTitleCallback(new MainPanelTitleCallback() {

            @Override
            public void setTitle(String params) {
                //TODO how to get readable name from action  mainPanel.setTitle(tabName + " (" + params + ")");

            }
        });
        table.reload();
        return table.asWidget();

    }

    /*
     * private Widget createDetail(){ final Label label = new Label("Detail");
     * ScrollPanel wrapper = new ScrollPanel(label);
     * //dynamicTreeWrapper.setSize("500px", "300px"); return wrapper; }
     */

}
